/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __parse_endian.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/13 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __parse_endian(char *p_in, int i_inlen, int i_real_len, LITTLE_BIG_ENDIAN i_endian, char **pp_out);

static void case1(void)
{
    int i_ret = 0;
    char *p_in = (char *)"\x03\x05";
    int i_inlen = 4;
    int i_real_len = 2;
    LITTLE_BIG_ENDIAN i_endian = BYTE_LITTLE_ENDIAN;
    char *p_out = NULL;

    i_ret = __parse_endian(p_in,i_inlen,i_real_len,i_endian,&p_out);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_TRUE(memcmp(p_out,"\x03\x05\x00\x00",i_inlen) == 0);

    frfree(p_out);
    return;
}

static void case2(void)
{
    int i_ret = 0;
    char *p_in = (char *)"\x03\x05";
    int i_inlen = 4;
    int i_real_len = 2;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    char *p_out = NULL;

    i_ret = __parse_endian(p_in,i_inlen,i_real_len,i_endian,&p_out);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_TRUE(memcmp(p_out,"\x00\x00\x03\x05",i_inlen) == 0);

    frfree(p_out);
    return;
}

static void case3(void)
{
    int i_ret = 0;
    char *p_in = (char *)"\x10";
    int i_inlen = 2;
    int i_real_len = 1;
    LITTLE_BIG_ENDIAN i_endian = BYTE_LITTLE_ENDIAN;
    char *p_out = NULL;

    i_ret = __parse_endian(p_in,i_inlen,i_real_len,i_endian,&p_out);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_TRUE(memcmp(p_out,"\x10\x00",i_inlen) == 0);

    frfree(p_out);
    return;
}

static void case4(void)
{
    int i_ret = 0;
    char *p_in = (char *)"\x10";
    int i_inlen = 2;
    int i_real_len = 1;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    char *p_out = NULL;

    i_ret = __parse_endian(p_in,i_inlen,i_real_len,i_endian,&p_out);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_TRUE(memcmp(p_out,"\x00\x10",i_inlen) == 0);

    frfree(p_out);
    return;
}

static void case5(void)
{
    int i_ret = 0;
    char *p_in = (char *)"\x09\x08\x07\x06";
    int i_inlen = 4;
    int i_real_len = 4;
    LITTLE_BIG_ENDIAN i_endian = BYTE_LITTLE_ENDIAN;
    char *p_out = NULL;

    i_ret = __parse_endian(p_in,i_inlen,i_real_len,i_endian,&p_out);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_TRUE(memcmp(p_out,"\x09\x08\x07\x06",i_inlen) == 0);

    frfree(p_out);
    return;
}

static void case6(void)
{
    int i_ret = 0;
    char *p_in = (char *)"\x09\x08\x07\x06";
    int i_inlen = 4;
    int i_real_len = 4;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    char *p_out = NULL;

    i_ret = __parse_endian(p_in,i_inlen,i_real_len,i_endian,&p_out);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    CU_ASSERT_TRUE(memcmp(p_out,"\x09\x08\x07\x06",i_inlen) == 0);

    frfree(p_out);
    return;
}

int add_suite__parse_endian(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frcomm __parse_endian()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
    CU_add_test(pSuite, "case5", case5);
    CU_add_test(pSuite, "case6", case6);

    return FR_SUCC;
}
